﻿using Max.PH4_2102A.Shopping.Domain;
using Max.PH4_2102A.Shopping.DTO;
using Max.PH4_2102A.Shopping.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH4_2102A.Shopping.API.Controllers
{
    /// <summary>
    /// 订单控制器
    /// </summary>
    [Route("order")]
    [ApiController]
    public class OrderController : ControllerBase
    {

        IOrderMasterService _service;

        NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();

        public OrderController(IOrderMasterService service)
        {
            _service = service;
        }


        #region 显示

        /*
            //验证参数
            //统一返回值
            //调用业务
         * **/

        /// <summary>
        /// 获取订单数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get(string? orderNo="")
        {
            //try
            //{
            //    int a = 1, b = 0;
            //    int c = a / b;

            //    log.Info("正常运行");
            //}
            //catch (Exception ex)
            //{
            //    log.Error(ex.Message);
            //    throw;
            //}

            //判断参数
            orderNo = orderNo ?? "";

            //统一返回值
            ResponseResultDto response = new();

            //调用业务
            var data = _service.GetOrderList(orderNo);
            response.Data = data;

            return Ok(response);
        }


        /// <summary>
        /// 获取所有订单信息-带分页
        /// </summary>
        /// <param name="orderNo"></param>
        /// <param name="state"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("all")]
        public IActionResult GetAll(string? orderNo="",
            int state=-1,int pageIndex = 1,int pageSize = 10)
        {
            ResponseResultDto response = new();

            _ = pageIndex <= 0 ? 1 : pageIndex;
            _ = pageSize <= 0 ? 10 : pageSize;

            var result = _service.GetOrderAll(orderNo, state, pageIndex, pageSize, out int total);
            response.Data = new { items = result, total };

            return Ok(response);
        }


        /// <summary>
        /// /枚举下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet("state")]
        public IActionResult GetStateEnum()
        {
            ResponseResultDto response = new();
            List<dynamic> list = new List<dynamic>();

            //获取枚举的类型
            var t = typeof(OrderStateEnum);
            //获取所有字段
            var fs = t.GetFields();
            fs.ToList().ForEach(x => {
                //Console.WriteLine(x.Name);

                if (!x.Name.Equals("value__"))
                {
                    var v = (int)Enum.Parse(t, x.Name);

                    list.Add(new { value = v,label = x.Name }); //枚举中定义是中文，所以直接获取名称
                }
            });

            response.Data = list;

            return Ok(response);
        }

        #endregion

        #region 添加


        /// <summary>
        /// EF+DTO实现创建订单
        /// </summary>
        /// <returns></returns>
        [HttpPost("ef")]
        public IActionResult CreateOrder(OrderInputDto dto)
        {
            _service.CreateOrderByDto(dto);

            return Ok();
        }

        /// <summary>
        /// 工作单元+DTO实现创建订单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("unit")]
        public IActionResult CreateOrderByUnitOfWork(OrderInputDto dto)
        {

            ResponseResultDto response = new();
            _service.CreateOrderByUnitOfWork(dto);

            return Ok(response);
        }

        #endregion
    }
}
